<?php
namespace app\common\controller;

use AlibabaCloud\Client\AlibabaCloud;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use think\Controller;
use think\Request;

class BaseController extends Controller{

    protected $request;
    protected $error;
    protected $validate = [
                'size'  => 307200,
                'ext'   => 'png,jpg',
                'type'  => 'image/png,image/jpeg',
              ];

    public function __construct(Request $request = NULL) {
        parent::__construct($request);
        header("content-type:text/html;charset=UTF-8");
        $this->request = $request;
    }
//    protected function _initialize()
//    {
//        header("content-type:text/html;charset=UTF-8");
//        $this->request = Request::instance();
//    }

    protected function upload_file($validate = [],$dir = 'upload',$item = 'logo',$rule = 'date'){

        // 获取上传文件配置定义
        $dir  = \think\Config::get('upload_file_dir')?:$dir;
        $item = \think\Config::get('upload_file_item')?:$item;
        $rule = \think\Config::get('upload_file_rule')?:$rule;
        // 获取上传文件验证数组定义
        //$validate_config = \think\Config::get('upload_file')?\think\Config::get('upload_file'):[];
        $validate_config = \think\Config::get('upload_file')?:[];
        $validate = array_merge($this->validate,$validate_config,$validate);

        // 判断是否是POST请求，如果不是则没有上传文件，返回一个null的空值
        if(!$this->request->isPost())     return ;

        // 如果是POST上传，那么接收上传的表单
        $file = $this->request->file($item);// 接上传文件的方法

        // 首先判断是否超过php.ini最大限制，如果超过则返回false并记录错误信息，如果没有上传则继续判断
        if(isset($_FILES[$item]['error']) && 1 == $_FILES[$item]['error']){
            $this->error = '上传文件超过PHP的最大限制！';
            return false;
        }

        // 如果没有上传返回null
        if(is_null($file))      return ;

        // 上传成功返回一个对象，失败返回false
        $info = $file->rule($rule)->validate($validate)->move($dir);

        // 如果上传失败，记录错误信息
        if(false == $info){
            $this->error = $file->getError();
        }

        return $info;
    }


//阿里云短信
public function alisms($phone,$code){
    $config=\think\Config::get('aliyunsms');
    $code=['code'=>$code];
    $code=json_encode($code);
    AlibabaCloud::accessKeyClient($config['accessKeyId'],$config['accessKeySecret'])
        ->regionId('cn-hangzhou')
        ->asDefaultClient();
    try {
        $result = AlibabaCloud::rpc()
            ->product('Dysmsapi')
            ->version('2017-05-25')
            ->action('SendSms')
            ->method('POST')
            ->host('dysmsapi.aliyuncs.com')
            ->options([
                'query' => [
                    'RegionId' => "cn-hangzhou",
                    'PhoneNumbers' => $phone,
                    'SignName' => $config['SignName'],
                    'TemplateCode' => $config['TemplateCode'],
                    'TemplateParam'=>$code,
                ],
            ])
            ->request();
        print_r($result->toArray());
    } catch (ClientException $e) {
        echo $e->getErrorMessage() . PHP_EOL;
    } catch (ServerException $e) {
        echo $e->getErrorMessage() . PHP_EOL;
    }

}
}